import requests
import base64
import urllib

#获取密钥
res = requests.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=RQSCANnqoAs9Bk1cTnOWSema&client_secret=G318YSye2P5vxv5g7xBfkwO9NOjBFd72")  #这个url是由https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials，加client_id=，加应用API Key，加client_secret=，加应用的Secret Key

token = res.json()['access_token']
# 定义头部信息
headers = {
    'Content-Type': 'application/x-www-from/urlencoded'
}
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + token

# 读取图片
my_img = open('../code1.png', 'rb')
tem_img = my_img.read()
my_img.close()
# 进行base64编码
temp_data = {'image': base64.b64encode(tem_img)}
# 对图片地址进行urlencode操作
temp_data = urllib.parse.urlencode(temp_data)

# 请求视图接口
res = requests.post(url=url, data=temp_data, headers=headers)
code = res.json()['words_result']
# code = res.json()['words_result'][2]['words'].replace(' ', '')
print(code)
# print(res.json())
str1=''
for i in code:
    value=i['words']
    str1+=value
print(str1)
